package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Count;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/count")
public class CountController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private UserService userService;


    @GetMapping("/list")
    public R<Count> get(Integer id) {
        log.info("id的数据{}",id);
        Count count = new Count();
        if (id == 1){
            count.setStartTime(LocalDate.now());
            LambdaQueryWrapper<Orders> lwq = new LambdaQueryWrapper<>();
            lwq.between(Orders::getOrderTime,count.getStartTime(), LocalDateTime.now());
            lwq.eq(Orders::getStatus,4);//4是已完成
            int count1 = ordersService.count(lwq);
//            System.out.println(count1);  今日已完成订单总和
            count.setSumOrder(count1);

            //今日收入总额
            Double sumByToday = ordersService.sumAmount();
            if (sumByToday == null){
                count.setSumMoney(0.0);
            }else {
                count.setSumMoney(sumByToday);
            }

            //用户总额
            Integer sumUser = userService.sumUser();

            System.out.println(sumUser);
//            System.out.println(sumUser);  用户总和
            if (sumUser == null){
                count.setSumUser(0);
            }else {
                count.setSumUser(sumUser);
            }

            //新增用户
            LambdaQueryWrapper<User> add = new LambdaQueryWrapper<>();
            add.like(User::getCreateTime,count.getStartTime());
            int addUser = userService.count(add);
            count.setAddUser(addUser);

            //取出每一天的营业额和时间
            List<Orders> moeny = ordersService.moeny();
            TreeMap<String,Object> map = new TreeMap<>();
            if(moeny.size() == 0){
                LocalDate now = LocalDate.now();
                String string = now.toString();
                String substring = string.substring(6);
                map.put(substring,0);
            }else {
                for (Orders orders : moeny) {
                    BigDecimal amount = orders.getAmount();
                    LocalDateTime orderTime = orders.getOrderTime();
                    String string = orderTime.toString();
                    String[] ts = string.split("T");
                    String substring = ts[0].substring(6);
                    System.out.println(amount);
                    System.out.println(substring);

                    map.put(substring,amount);
                }
            }

            count.setDaysWithMoney(map);

            //每日用户增长趋势
            Map<String,Object>  user = userService.user();
            TreeMap<String,Object> map1 = new TreeMap<>();
            if (user == null){
                LocalDate now = LocalDate.now();
                String string = now.toString();
                String substring = string.substring(6);
                map1.put(substring,0);
            }else {
                for (String key : user.keySet()) {
                    System.out.println("key= "+ key + " and value= " + user.get(key));
                    Date create_time = (Date) user.get("create_time");
                    String string = create_time.toString();
                    String[] ts = string.split(" ");
                    String substring = ts[0].substring(6);
                    Object number = user.get("number");

                    map1.put(substring,number);
                }
            }
            count.setDaysWithUser(map1);
        }
        else if (id == 2){
            count.setStartTime(LocalDate.now().plusDays(-1));
            LambdaQueryWrapper<Orders> lwq = new LambdaQueryWrapper<>();
            lwq.between(Orders::getOrderTime,count.getStartTime(),LocalDateTime.now());
            lwq.eq(Orders::getStatus,4);
            int count1 = ordersService.count(lwq);
//            System.out.println(count1);  昨日已完成订单总和
            count.setSumOrder(count1);

            //昨日收入总额
            Double sumByToday =ordersService.sumDaysAmount();
            if (sumByToday == null){
                count.setSumMoney(0.0);
            }else {
                count.setSumMoney(sumByToday);
            }

            //新增用户
            LambdaQueryWrapper<User> add = new LambdaQueryWrapper<>();
            add.like(User::getCreateTime,count.getStartTime());
            int addUser = userService.count(add);
            count.setAddUser(addUser);

            System.out.println("===========================");
            LocalDate now = LocalDate.now();
            System.out.println(now);
            LambdaQueryWrapper<User> add2 = new LambdaQueryWrapper<>();
            add2.like(User::getCreateTime,now);
            int addUser2 = userService.count(add2);
            System.out.println(addUser2);

            System.out.println("==================================");
            //用户总额
            Integer sumUser = userService.sumUser();
//            System.out.println(sumUser);  用户总和
            if (sumUser == null){
                count.setSumUser(0);
            }else {
                Integer sumDayUser = sumUser - addUser2;
                System.out.println(sumDayUser);
                count.setSumUser(sumDayUser);
            }

            //取出每一天的营业额和时间
            List<Orders> moeny = ordersService.dayMoeny();
            TreeMap<String,Object> map = new TreeMap<>();
            if(moeny.size() == 0){
                String string = now.toString();
                String substring = string.substring(6);
                map.put(substring,0);
            }else {
                for (Orders orders : moeny) {
                    BigDecimal amount = orders.getAmount();
                    LocalDateTime orderTime = orders.getOrderTime();
                    String string = orderTime.toString();
                    String[] ts = string.split("T");
                    String substring = ts[0].substring(6);
                    System.out.println(amount);
                    System.out.println(substring);

                    map.put(substring,amount);
                }
            }

            count.setDaysWithMoney(map);

            //每日用户增长趋势
            Map<String,Object>  user = userService.dayUser();

            TreeMap<String,Object> map1 = new TreeMap<>();
            if (user == null){
                String string = count.getStartTime().toString();
                String substring = string.substring(6);
                map1.put(substring,0);
            }else {
                for (String key : user.keySet()) {
                    System.out.println("key= "+ key + " and value= " + user.get(key));
                    Date create_time = (Date) user.get("create_time");
                    String string = create_time.toString();
                    String[] ts = string.split(" ");
                    String substring = ts[0].substring(6);
                    Object number = user.get("number");
                    map1.put(substring,number);
                }
            }
            count.setDaysWithUser(map1);

        }
        else if (id == 3){
            //近7天
            count.setStartTime(LocalDate.now().plusDays(-6));
            LambdaQueryWrapper<Orders> lwq = new LambdaQueryWrapper<>();
            lwq.between(Orders::getOrderTime,count.getStartTime(),LocalDateTime.now());
            lwq.eq(Orders::getStatus,4);
            int count1 = ordersService.count(lwq);
//            System.out.println(count1);  今日已完成订单总和
            count.setSumOrder(count1);

            //今日收入总额
            Double sumByToday = ordersService.sumWeekAmount();
            if (sumByToday == null){
                count.setSumMoney(0.0);
            }else {
                count.setSumMoney(sumByToday);
            }

            //用户总额
            Integer sumUser = userService.sumUser();

            System.out.println(sumUser);
//            System.out.println(sumUser);  用户总和
            if (sumUser == null){
                count.setSumUser(0);
            }else {
                count.setSumUser(sumUser);
            }

            //新增用户
            LambdaQueryWrapper<User> add = new LambdaQueryWrapper<>();
//            add.like(User::getCreateTime,count.getStartTime());
            add.between(User::getCreateTime,count.getStartTime(),LocalDateTime.now());

            int addUser = userService.count(add);
            count.setAddUser(addUser);

            //取出每一天的营业额和时间
            List<Orders> moeny = ordersService.dayWeekMoeny();
            TreeMap<String,Object> map = new TreeMap<>();
            if(moeny.size() == 0){
                LocalDate now = LocalDate.now();
                String string = now.toString();
                String substring = string.substring(6);
                map.put(substring,0);
            }else {
                for (Orders orders : moeny) {
                    BigDecimal amount = orders.getAmount();
                    LocalDateTime orderTime = orders.getOrderTime();
                    String string = orderTime.toString();
                    String[] ts = string.split("T");
                    String substring = ts[0].substring(6);
                    System.out.println(amount);
                    System.out.println(substring);

                    map.put(substring,amount);
                }
            }

            count.setDaysWithMoney(map);

            //每日用户增长趋势
            List<Map<String, Object>>  user = userService.dayWeekUser();
            TreeMap<String,Object> userWithDay = new TreeMap<String,Object>();
            for (Map<String, Object> userMap : user) {
                for (String key : userMap.keySet()) {
                    System.out.println("key= "+ key + " and value= " + userMap.get(key));
                    LocalDateTime create_time = (LocalDateTime) userMap.get("create_time");
                    String string = create_time.toString();
                    String[] ts = string.split(" ");
                    String substring = ts[0].substring(6);
                    Object number = userMap.get("number");
                    userWithDay.put(substring,number);
                }
            }
            count.setDaysWithUser(userWithDay);


            /*LambdaQueryWrapper<User> lwq1 = new LambdaQueryWrapper<>();
            lwq1.between(User::getCreateTime,count.getStartTime(),LocalDateTime.now());
            List<User> list = userService.list(lwq1);
            List<String> list1 = new ArrayList<>();

            for (User user : list) {
                LocalDateTime createTime = user.getCreateTime();
                String string = createTime.toString();
                String[] ts = string.split("T");
                String substring = ts[0].substring(6);
                System.out.println(substring);
                list1.add(substring);
            }
            for (int i = 0; i < list1.size(); i++) {
                if ()
                System.out.println(list1.get(i));
            }*/
        }
        else if(id == 4){
            //近30天
            count.setStartTime(LocalDate.now().plusDays(-29));
            LambdaQueryWrapper<Orders> lwq = new LambdaQueryWrapper<>();
            lwq.between(Orders::getOrderTime,count.getStartTime(),LocalDateTime.now());
            lwq.eq(Orders::getStatus,4);
            int count1 = ordersService.count(lwq);
//            System.out.println(count1);  今日已完成订单总和
            count.setSumOrder(count1);

            //今日收入总额
            Double sumByToday = ordersService.sumMonthAmount();
            if (sumByToday == null){
                count.setSumMoney(0.0);
            }else {
                count.setSumMoney(sumByToday);
            }

            //用户总额
            Integer sumUser = userService.sumUser();

            System.out.println(sumUser);
//            System.out.println(sumUser);  用户总和
            if (sumUser == null){
                count.setSumUser(0);
            }else {
                count.setSumUser(sumUser);
            }

            //新增用户
            LambdaQueryWrapper<User> add = new LambdaQueryWrapper<>();
//            add.like(User::getCreateTime,count.getStartTime());
            add.between(User::getCreateTime,count.getStartTime(),LocalDateTime.now());
            int addUser = userService.count(add);
            count.setAddUser(addUser);

            //取出每一天的营业额和时间
            List<Orders> moeny = ordersService.dayMonthMoeny();
            TreeMap<String,Object> map = new TreeMap<>();
            if(moeny.size() == 0){
                LocalDate now = LocalDate.now();
                String string = now.toString();
                String substring = string.substring(6);
                map.put(substring,0);
            }else {
                for (Orders orders : moeny) {
                    BigDecimal amount = orders.getAmount();
                    LocalDateTime orderTime = orders.getOrderTime();
                    String string = orderTime.toString();
                    String[] ts = string.split("T");
                    String substring = ts[0].substring(6);
                    System.out.println(amount);
                    System.out.println(substring);

                    map.put(substring,amount);
                }
            }

            count.setDaysWithMoney(map);

            //每日用户增长趋势
            List<Map<String, Object>>  user = userService.dayMonthUser();
            TreeMap<String,Object> userWithDay = new TreeMap<String,Object>();
            for (Map<String, Object> userMap : user) {
                for (String key : userMap.keySet()) {
                    System.out.println("key= "+ key + " and value= " + userMap.get(key));
                    Date create_time = (Date) userMap.get("create_time");
                    String string = create_time.toString();
                    String[] ts = string.split(" ");
                    String substring = ts[0].substring(6);
                    Object number = userMap.get("number");
                    userWithDay.put(substring,number);
                }
            }
            count.setDaysWithUser(userWithDay);
        }
        return R.success(count);
    }
}
